package com.egene.zh.activity.member;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;

import com.egene.zh.R;
import com.egene.zh.activity.wiki.WikInfoActivity;
import com.egene.zh.activity.wiki.WikInfoActivity_;
import com.egene.zh.api.EgeneApi;
import com.egene.zh.dao.UserDao;
import com.egene.zh.model.HealthItem;
import com.egene.zh.model.HealthList;
import com.egene.zh.utils.JsonUtil;
import com.egene.zh.utils.MToast;
import com.egene.zh.ui.HealthView;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

@EFragment(R.layout.fragment_health)
public class HealthFragment extends Fragment implements HealthView.OnItemClickListener {
    public final String TYPE_PERSONAL = "1";//type:1 个性化 2 营养食谱 3 运动养生
    public final String TYPE_FOOD_LIST = "2";
    public final String TYPE_SPORT = "3";
    public final String TYPE_PERSONAL_PROJECT = "4";
//    private final String TITLE_PERSONAL = "个性化";//type:1 个性化 2 营养食谱 3 运动养生
//    private final String TITLE_FOOD_LIST = "营养食谱";
//    private final String TITLE_SPORT = "运动养生";

    @ViewById
    HealthView hvPersonalize;
    @ViewById
    HealthView hvFoodList;
    @ViewById
    HealthView hvSport;

    @Bean
    UserDao userDao;


    @AfterViews
    void initViews(){
        hvPersonalize.setOnItemClickListener(this);
        hvFoodList.setOnItemClickListener(this);
        hvSport.setOnItemClickListener(this);

        initMoreListener();
        fetchHealthList();
    }

    private void initMoreListener() {
        hvPersonalize.findViewById(R.id.tvMore).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String  string = getString(R.string.personal_health_message);
                HealthListActivity_.intent(getActivity()).type("4").title(string).start();
            }
        });
        hvFoodList.findViewById(R.id.tvMore).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String  string = getString(R.string.recommend_foods);
                HealthListActivity_.intent(getActivity()).type("1").title(string).start();
            }
        });
        hvSport.findViewById(R.id.tvMore).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String  string = getString(R.string.recommend_sport);
                HealthListActivity_.intent(getActivity()).type("2").title(string).start();
            }
        });
    }

    void fetchHealthList(){
        //d424e0467e4b4e1684df9e42befdddaa

        EgeneApi.getHealthList(userDao.getUser().getUserID(), new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                HealthList result = JsonUtil.parse(responseBody, HealthList.class);
                if(result != null){
                    if(result.isSuccess()){
                        hvPersonalize.setData(R.drawable.personalise, getActivity().getString(R.string.personal_health_message), null);
                        hvFoodList.setData(R.drawable.food_list, getActivity().getString(R.string.recommend_foods), result.getPersonalitylist());
                        hvSport.setData(R.drawable.sport, getActivity().getString(R.string.recommend_sport), result.getNutritionlist());
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                MToast.show(R.string.no_network);
            }
        });
    }

    @Override
    public void onItemClick(HealthItem item) {
        Intent intent = new Intent(getActivity(), WikInfoActivity_.class);
        intent.putExtra(WikInfoActivity.ID, "" + item.getArticleid());
        startActivity(intent);
    }
}
